package com.jp.knowledge.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.my.fragment.IntegralFragment;
import com.jp.knowledge.my.model.HelpCateModel;
import com.jp.knowledge.my.view.IntegralTabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/6/13.
 */

public class IntegralActivity extends SlidingActivity implements View.OnClickListener {

    @ViewInject(R.id.integral_num)
    private TextView integralNum;
    @ViewInject(R.id.integral_earn)
    private TextView integralEarn;
    @ViewInject(R.id.integral_buy)
    private TextView integralBuy;
    @ViewInject(R.id.integral_tab)
    private IntegralTabView integralTab;
    @ViewInject(R.id.integral_pager)
    private ViewPager integralPager;

    private List<Fragment> fragments;

    private LocalBroadCast localBroadCast;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected int getResourceId() {
        return R.layout.activity_integral;
    }

    @Override
    protected void init() {
        initTitle();
        initBroadcase();
        integralNum.setText(application.getUserData().getIntegralNum()+"");
        fragments = new ArrayList<>();
        fragments.add(IntegralFragment.newInstance(1));
//        fragments.add(IntegralFragment.newInstance(2));
        fragments.add(IntegralFragment.newInstance(3));
        integralEarn.setOnClickListener(this);
        integralBuy.setOnClickListener(this);
        integralTab.setOnTabSelectedListener(new IntegralTabView.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                integralPager.setCurrentItem(index);
            }
        });
        integralPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        integralPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) {}
            @Override
            public void onPageSelected(int position) {
                integralTab.setSelectedTab(position);
            }
        });
    }

    private void initBroadcase(){
        localBroadcastManager = LocalBroadcastManager.getInstance(mContext);
        localBroadCast = new LocalBroadCast(new LocalBroadCast.BroadCastCallBack() {
            @Override
            public void done(Context context, Intent intent) {
                if(LocalBroadCast.GET_LOGIN_INFO.equals(intent.getAction())) {
                    integralNum.setText(application.getUserData().getIntegralNum() + "");
                    for(Fragment fragment : fragments)
                        ((IntegralFragment)fragment).autoRefresh();
                }
            }
        });
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LocalBroadCast.GET_LOGIN_INFO);
        localBroadcastManager.registerReceiver(localBroadCast, intentFilter);
    }

    @Override
    protected void onDestroy() {
        localBroadcastManager.unregisterReceiver(localBroadCast);
        super.onDestroy();
    }

    private void initTitle(){
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setOnClickListener(this);
        rightIcon.setVisibility(View.GONE);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("充值");
        rightBtn.setOnClickListener(this);
        topName.setText("我的知识币");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.right_bn:
                startActivity(new Intent(mContext, IntegralBuyActivity.class));
                break;
            case R.id.integral_earn:
                startActivity(new Intent(mContext, HelpCatePageActivity.class)
                        .putExtra("pageId", "b5c497f6-f1de-f208-060a-7b2804b0268f")
                        .putExtra("title", integralEarn.getText()));
                break;
            case R.id.integral_buy:
                break;
        }
    }
}
